package de.doegel.common;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;

import de.doegel.common.model.Contact;
import de.doegel.common.model.IncomingMessage;
import de.doegel.common.model.News;
import de.doegel.common.model.OutgoingMessage;

public abstract class AbstractDatabaseHelper extends OrmLiteSqliteOpenHelper {
	private Dao<Contact, Integer> contactDao = null;
	private Dao<News, Integer> newsDao = null;
	private Dao<IncomingMessage, Integer> inboxDao = null;
	private Dao<OutgoingMessage, Integer> outboxDao = null;

	public AbstractDatabaseHelper(Context context, String databaseName, CursorFactory factory, int databaseVersion) {
		super(context, databaseName, factory, databaseVersion);
	}

	public Dao<Contact, Integer> getContactDao() throws SQLException {
		if (contactDao == null) {
			contactDao = getDao(Contact.class);
		}
		return contactDao;
	}

	public Dao<News, Integer> getNewsDao() throws SQLException {
		if (newsDao == null) {
			newsDao = getDao(News.class);
		}
		return newsDao;
	}

	public Dao<IncomingMessage, Integer> getInboxDao() throws SQLException {
		if (inboxDao == null) {
			inboxDao = getDao(IncomingMessage.class);
		}
		return inboxDao;
	}

	public Dao<OutgoingMessage, Integer> getOutboxDao() throws SQLException {
		if (outboxDao == null) {
			outboxDao = getDao(OutgoingMessage.class);
		}
		return outboxDao;
	}
	
	@Override
	public void close() {
		super.close();
		contactDao = null;
		newsDao = null;
		inboxDao = null;
		outboxDao = null;
	}
}
